package hv.fmw.core.command 
{
	import flash.events.EventDispatcher;
	
	/**
	 * ...
	 * @author hvong
	 */
	public class ParallelCommand extends EventDispatcher
	{
		private var _commands:Array = [];
		private var _index:int = 0;
		
		public function ParallelCommand() {
			
		}
		
		public function addCommand(command:Command):void {
			_commands.push(command);
		}
		
		public function execute():void {
			var l:int = _commands.length;
			var command:Command;
			for (var i:int = 0; i < l; i++ ) {
				command = _commands[i];
				command.addEventListener(CommandEvent.COMMAND_COMPLETE, _onCommandComplete);
				ICommand(command).execute();
			}
		}
		
		private function _onCommandComplete(event:CommandEvent):void {
			dispatchEvent(new CommandEvent(CommandEvent.COMMAND_COMPLETE, event.target as Command));
			if (++_index==_commands.length) {
				dispatchEvent(new CommandEvent(CommandEvent.ALL_COMMAND_COMPLETE));
			}
		}
	}
}